package com.zero.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.entity.FamilyMember;
import com.zero.entity.IncomeType;
import com.zero.mapper.FamilyMemberMapper;
import com.zero.mapper.IncomeTypeMapper;
import com.zero.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Service
public class IncomeTypeService {


    @Autowired
    private IncomeTypeMapper incomeTypeMapper;

    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    public Result add(IncomeType incomeType, HttpServletRequest request){
        if (incomeType == null)
            return Result.fail("添加支出分类失败");

        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null || familyMember.getId() == null)
            return Result.fail("获取不到用户信息");
        familyMember = familyMemberMapper.selectById(familyMember.getId());

        incomeType.setFamilyId(familyMember.getFamilyId());
        int row = incomeTypeMapper.add(incomeType);
        if (row == 1)
            return Result.ok("添加收入分类成功");
        return Result.fail("添加收入分类失败");
    }

    @Transactional
    public Result delete(String ids){
        if (ids == null)
            return Result.fail("无法获取要删除的项");
        String[] idSet = ids.split(",");
        int row = 0;
        for (String id : idSet) {
            row += incomeTypeMapper.delete(Integer.parseInt(id));
        }
        if (row > 0)
            return Result.ok("成功删除"+ row +"条记录");
        return Result.fail("删除失败");
    }

    public Result update(IncomeType incomeType){
        if (incomeType == null)
            return Result.fail("获取修改信息失败");
        int row = incomeTypeMapper.update(incomeType);
        if (row == 1)
            return Result.ok("修改支出分类成功");
        return Result.fail("修改支出分类失败");
    }

    public Result select(IncomeType incomeType, HttpServletRequest request){
        if (incomeType == null )
            return Result.fail("获取不到查询的详细信息");

        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null || familyMember.getId() == null)
            return Result.fail("获取不到用户信息");
        familyMember = familyMemberMapper.selectById(familyMember.getId());

        incomeType.setFamilyId(familyMember.getFamilyId());

        //分页查询信息
        if (incomeType.getPage() != null ){
            if (incomeType.getLimit() != null)
                PageHelper.startPage(incomeType.getPage(), incomeType.getLimit());
            else
                PageHelper.startPage(incomeType.getPage(), 10);
        }else{
            PageHelper.startPage(1, 10);
        }

        List<IncomeType> incomeTypes = incomeTypeMapper.select(incomeType);
        PageInfo pageInfo = new PageInfo(incomeTypes);

        return Result.ok(incomeTypes, pageInfo.getTotal());

    }

    public Result select(HttpServletRequest request){
        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null || familyMember.getId() ==null)
            return Result.fail("获取不到用户信息");
        familyMember = familyMemberMapper.selectById(familyMember.getId());

        IncomeType incomeType = new IncomeType();
        incomeType.setFamilyId(familyMember.getFamilyId());

        List<IncomeType> incomeTypes = incomeTypeMapper.select(incomeType);
        return Result.ok(incomeTypes);
    }
}
